//
//  NewfeatureViewController.swift
//  SwiftWB
//
//  Created by 钟清 on 17/3/30.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit
import SnapKit

class NewfeatureViewController: UIViewController {

    /* 新特性页面最大数 */
    private var maxCount = 4
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

}

extension NewfeatureViewController: UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxCount
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        /* 获取cell */
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newfeatureCell", forIndexPath: indexPath) as! SWNewfeatureCell
        cell.backgroundColor = (indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor())
        /* 设置数据 */
        cell.index = indexPath.item
        /* 返回cell */
        return cell
    }
    
}

extension NewfeatureViewController: UICollectionViewDelegate {
    func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) { //  传入的cell和indexPath是前一个的
        let index = collectionView.indexPathsForVisibleItems().last!
        let currentCell = collectionView.cellForItemAtIndexPath(index) as! SWNewfeatureCell
        if index.item == (maxCount - 1) {
            /* 最后一页做动画 */
            currentCell.startAnimation()
        }
    }
    
}

// MARK: - 自定义Cell
class SWNewfeatureCell: UICollectionViewCell {
    var index: Int = 0
        {
        didSet{
            let name = "new_feature_\(index+1)"
            iconView.image = UIImage(named: name)
            startButton.hidden = true
        }
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    /* 动画 */
    func startAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: {
            self.startButton.transform = CGAffineTransformIdentity
            }, completion: { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        })

    }
    /* 内部控制方法 */
    private func setUpUI() {
        contentView.addSubview(iconView)
        iconView.userInteractionEnabled = true
        contentView.addSubview(startButton)
        iconView.backgroundColor = UIColor.whiteColor()
        iconView.snp_makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        startButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(-160)
        }
    }
    /* 懒加载 */
    private lazy var iconView = UIImageView()
    private lazy var startButton: UIButton = {
        let btn = UIButton(imageName: nil, backgroundImageName: "new_feature_button")
        btn.addTarget(self, action: #selector(SWNewfeatureCell.startButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    @objc private func startButtonClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: true)
    }
}
// MARK: - 自定义布局
class SWNewfeatureLayout: UICollectionViewFlowLayout {
    override func prepareLayout() {
        /* cell的尺寸 */
        itemSize = UIScreen.mainScreen().bounds.size
        /* cell的间隙 */
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        /* 滚动方向 */
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        /* 设置分页 */
        collectionView?.pagingEnabled = true
        /* 禁用回弹效果 */
        collectionView?.bounces = false
        /* 取出滚动条 */
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
    }
}

